package com.louis.epay.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.louis.epay.alipay.AlipayConfig2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.louis.epay.alipay.AlipayBean;
import com.louis.epay.service.PayService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.louis.epay.alipay.AlipayProperties.*;

/**
 * 订单接口
 *
 */
@RestController()
@RequestMapping("order")
public class OrderController {

	@Autowired
	private PayService payService;

	/**
	 * 阿里支付
	 * @param
	 * @param subject
	 * @param
	 * @param body
	 * @return
	 * @throws AlipayApiException
	 */
	@PostMapping(value = "alipay")
	public String alipay(String outTradeNo, String subject, String totalAmount, String body) throws AlipayApiException {
		AlipayBean alipayBean = new AlipayBean();
		alipayBean.setOut_trade_no(outTradeNo);
		alipayBean.setSubject(subject);
		alipayBean.setTotal_amount(totalAmount);
		alipayBean.setBody(body);
		return payService.aliPay(alipayBean);
	}

/*	@PostMapping(value = "paypay")
	public void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws ServletException, IOException {

		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, PRIVARY_KEY, "json", CHARSET, PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
		alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
		alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
		alipayRequest.setBizContent("{" +
				" \"out_trade_no\":\"20150320010101002\"," +
				" \"total_amount\":\"88.88\"," +
				" \"subject\":\"Iphone6 16G\"," +
				" \"product_code\":\"QUICK_WAP_PAY\"" +
				" }");//填充业务参数
		String form="";
		try {
			form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		httpResponse.setContentType("text/html;charset=" + CHARSET);
		httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
		httpResponse.getWriter().flush();
		httpResponse.getWriter().close();
	}*/
	@RequestMapping("paypay")
	public void doPost(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
			throws ServletException, IOException {
		try {

			AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig2.URL,         AlipayConfig2.APPID,
					AlipayConfig2.RSA_PRIVATE_KEY, "json", "UTF-8", AlipayConfig2.ALIPAY_PUBLIC_KEY, "RSA2");
			AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
			JSONObject order = new JSONObject();
			order.put("out_trade_no", "23242345rfg34534fertgedf");
			order.put("subject", "songSir支付");
			order.put("product_code", "QUICK_WAP_WAY");
			order.put("body", "儿童泳装|泳具");
			order.put("total_amount", "2000.00");
			order.put("subject", "竞浪男童平角泳裤");
			alipayRequest.setBizContent(order.toString());
			/**
			 * 在公共参数中设置回跳和通知地址
			 */
			alipayRequest.setNotifyUrl(AlipayConfig2.NOTIFY_URL);
			alipayRequest.setReturnUrl(AlipayConfig2.RETURN_URL);
			String form = alipayClient.pageExecute(alipayRequest).getBody();
			httpResponse.setContentType("text/html;charset=utf-8");
			httpResponse.getWriter().write(form);
			httpResponse.getWriter().flush();
			httpResponse.getWriter().close();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}




}
